﻿var maintainItemListTableVue = new Vue({
    el: "#maintainItemListTable",
    data: {
        pageModel: {
            pageIdx: 1,
            pageSize: 10,
            pageTotal: 0,
            pageLength: 0,
            reults: []
        },
        searchModel: {
            queryName: "",
            queryTypeID: -1,
            queryIsDisable: -1,

            orderByType: false,
            isAsc: true,
            isAll: true
        },
        typeDictionary: {},
        isDisableDictionary: {}
    },
    methods: {
        AddItem: function () {
            AddMaintainItem(this);
        },
        EditItem: function (itemID) {
            EditMaintainItem(this, itemID);
        },
        DisableItem: function (itemID, isDisable) {
            DisableMaintainItem(this, itemID, isDisable);
        },
        DeleteItem: function (item) {
            DeleteMaintainItem(this, item);
        },
        GotoPage: function (toPageIdx) {
            this.pageModel.pageIdx = toPageIdx;
            ListMaintainItemForPage(this);
        },
        SearchItem: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.isAll = false;
            ListMaintainItemForPage(this);
        },
        SearchReset: function () {
            this.pageModel.pageIdx = 1;
            this.searchModel.queryName = "";
            this.searchModel.queryTypeID = -1;
            this.searchModel.queryIsDisable = -1;
            this.searchModel.isAll = true;
            ListMaintainItemForPage(this);
        }
        
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.typeDictionary = GlobalDictionary.GetEquipmentTypeDictionary();
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();
        ListMaintainItemForPage(this);
    }
});

/**
 * 查询所有保养项目
 */
function ListMaintainItemForPage(vue) {
    vue.pageModel.results = [];
    ShowAzLoading();

    $.ajax({
        type: "POST",
        url: "/Maintain/ListMaintainItemForPage?v=" + Math.random(),
        data: {
            pageModel: vue.pageModel,
            searchModel: vue.searchModel
        },
        success: function (res) {
            if (res.status == "Success") {
                vue.pageModel = res.data;   
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 *新增保养项目
 */
function AddMaintainItem(vue) {
    OpenAzModal("新增保养项目New maintenance items", "/Maintain/Page/ItemDetail?IsNew=true", function () {
        ListMaintainItemForPage(vue);
    });
}

/**
 * 更新保养项目是否禁用
 */
function DisableMaintainItem(vue, itemID, isDisable) {
    ShowAzLoading();
    $.ajax({
        type: "POST",
        url: "/Maintain/DisableMaintainItem",
        data: { itemID: itemID, isDisable: isDisable },
        success: function (res) {
            if (res.status == "Success") {
                ListMaintainItemForPage(vue);
                toastr.success("更新成功Update succeeded。");
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            HideAzLoading();
            toastr.error("加载数据异常，请稍后再试或联系管理员Loading data is abnormal. Please try again later or contact the administrator。");
        }
    });
}

/**
 * 编辑保养项目信息
 */
function EditMaintainItem(vue, itemID) {
    OpenAzModal("编辑保养项目Edit maintenance item", "/Maintain/Page/ItemDetail?ItemID=" + itemID, function () {
        ListMaintainItemForPage(vue);
    });
}

/**
 * 删除保养项目信息
 */
function DeleteMaintainItem(vue, item) {
    var message = "确认删除保养项目Confirm to delete maintenance items【" + item.Name + "】?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Maintain/DeleteMaintainItem",
            data: { ItemID: item.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除保养项目信息成功Successfully deleted maintenance item information。");
                    ListMaintainItemForPage(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        })
    }
}